2018 Kakao Blind Recruitment

1> secret map (Lv. 1)
https://school.programmers.co.kr/learn/courses/30/lessons/17681
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
string and_num(int n1, int n2, const int& n){
string adn;
for(int i=pow(2, n-1); i>0; i/=2){
if(n1/i || n2/i) adn+="#";
else adn+=" ";
n1%=i; n2%=i;
}
return adn;
}
vector<string> solution(int n, vector<int> arr1, vector<int> arr2){
vector<string> answer;
for(int i=0; i<n; ++i){
answer.push_back(and_num(arr1[i], arr2[i], n));
}
return answer;
}
template <typename T>
void print_vec(vector<T> vec){
if(vec.size()==0){
cout<<"Empty Vector"<<endl;
return;
}
cout<<'\n';
for(int i=0; i<vec.size()-1; ++i){
cout<<vec[i]<<", "<<endl;
}
cout<<vec[vec.size()-1]<<endl;
}
int main(void){
int fn=5, sn=6;
vector<int> farr1={9, 20, 28, 18, 11};
vector<int> farr2={30, 1, 21, 17, 28};
vector<int> sarr1={46, 33, 33, 22, 31, 50};
vector<int> sarr2={27, 56, 19, 14, 14, 10};
vector<string> result1=solution(fn, farr1, farr2);
vector<string> result2=solution(sn, sarr1, sarr2);
print_vec<string>(result1);
print_vec<string>(result2);
return 0;
}
1> Cache (Lv. 1)
https://school.programmers.co.kr/learn/courses/30/lessons/17680
#include <iostream>
#include <vector>
using namespace std;
bool compare(string str1, string str2){
if(str1.length()!=str2.length()) return false;
for(int i=0; i<str1.length(); ++i){
if(tolower(str1[i])!=tolower(str2[i])) return false;
}
return true;
}
int solution(int cacheSize, vector<string> cities){
int answer=0;
for(int i=0; i<cities.size(); ++i){
answer+=5;
for(int j=i-cacheSize; j<i; ++j){
if(j<0) continue;
if(compare(cities[i], cities[j])){
answer-=4;
continue;
}
}
}
return answer;
}
int main(void){
vector<string> cities1={
"Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA"
};
vector<string> cities2={
"Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul", "Jeju", "Pangyo", "Seoul"
};
vector<string> cities3={
"Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"
};
vector<string> cities4={
"Jeju", "Pangyo", "Seoul", "NewYork", "LA", "SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"
};
vector<string> cities5={
"Jeju", "Pangyo", "NewYork", "newyork"
};
vector<string> cities6={
"Jeju", "Pangyo", "Seoul", "NewYork", "LA"
};
int result1, result2, result3, result4, result5, result6;
result1=solution(3, cities1);
result2=solution(3, cities2);
result3=solution(2, cities3);
result4=solution(5, cities4);
result5=solution(2, cities5);
result6=solution(0, cities6);
cout<<result1<<endl;
cout<<result2<<endl;
cout<<result3<<endl;
cout<<result4<<endl;
cout<<result5<<endl;
cout<<result6<<endl;
return 0;
}
1> news Clustering (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/17677
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const static int crt=65536;
void modify(vector<string>& vec_string, string str){
for(int i=0; i<str.length()-1; ++i){
char c1=tolower(str[i]), c2=tolower(str[i+1]);
if(c1>='a' && c1<='z' && c2>='a' &&c2<='z'){
string tmp{c1, c2};
vec_string.push_back(tmp);
}
}
}
int intersection(vector<string> v1, vector<string> v2){
int inter=0;
for(int i=0; i<v2.size(); ++i){
auto iter=find(v1.begin(), v1.end(), v2[i]);
if(iter!=v1.end()){
*iter="00";
inter++;
}
}
return inter;
}
int solution(string str1, string str2){
int answer=0;
vector<string> v_str1, v_str2;
modify(v_str1, str1);
modify(v_str2, str2);
int s_inter=intersection(v_str1, v_str2);
int s_union=v_str1.size()+v_str2.size()-s_inter;
if(s_union==0) answer=crt;
else answer=s_inter*crt/s_union;
return answer;
}
//
int main(void){
int result1, result2, result3, result4;
result1=solution("FRANCE", "french");
result2=solution("handshake", "shake hands");
result3=solution("aa1+aa2", "AAAA12");
result4=solution("E=M*C^2", "e=m*c^2");
cout<<result1<<endl;
cout<<result2<<endl;
cout<<result3<<endl;
cout<<result4<<endl;
}
1> Dart Game (Lv. 1)
https://school.programmers.co.kr/learn/courses/30/lessons/17682
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
int solution(string dartResult){
int answer=0, i=0, j=0;
vector<int> three;
vector<string> three_r;
while(i<dartResult.length()){
if(dartResult[i]>='0' && dartResult[i]<='9' && i){
if(i-j!=1){
three_r.push_back(dartResult.substr(j, i-j));
j=i;
}
}
i++;
if(i==dartResult.length()) three_r.push_back(dartResult.substr(j, i-j));
}
for(int i=0; i<three_r.size(); ++i){
int num=0, j=1, root;
string str;
if(three_r[i][1]>='0' && three_r[i][1]<='9'){
j=2;
num=stoi(three_r[i].substr(0, 2));
} else num=int(three_r[i][0]-'0');
switch(three_r[i][j]){
case 'S':
root=1;
break;
case 'D':
root=2;
break;
case 'T':
root=3;
break;
}
num=pow(num, root);
three.push_back(num);
if(three_r[i][three_r[i].length()-1]=='*'){
if(three.size()==1) three[0]*=2;
else {
three[three.size()-1]*=2;
three[three.size()-2]*=2;
}
} else if (three_r[i][three_r[i].length()-1]=='#'){
three[three.size()-1]*=-1;
}
}
for(int i=0; i<3; ++i){
answer+=three[i];
}
return answer;
}
int main(void){
string dartResult1="1S2D*3T";
string dartResult2="1D2S#10S";
string dartResult3="1D2S0T";
string dartResult4="1S*2T*3S";
string dartResult5="1D#2S*3S";
string dartResult6="1T2D3D#";
string dartResult7="1D2S3T*";
cout<<solution(dartResult1)<<endl;
cout<<solution(dartResult2)<<endl;
cout<<solution(dartResult3)<<endl;
cout<<solution(dartResult4)<<endl;
cout<<solution(dartResult5)<<endl;
cout<<solution(dartResult6)<<endl;
cout<<solution(dartResult7)<<endl;
return 0;
}
1> friends 4 blocks (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/17679
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct block{
vector<vector<int>> board;
int m, n;
block(){}
block(int _n, int _m, vector<string> sboard): n(_n), m(_m){
board=vector<vector<int>>(m, vector<int>(n, 0));
map<char, int> alpha;
int alpha_num=1;
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
char value=sboard[i][j];
if(alpha.find(value)==alpha.end()){
alpha[value]=alpha_num;
board[i][j]=alpha_num;
alpha_num++;
} else {
board[i][j]=alpha.at(value);
}
}
}
}
void drop(void){
for(int i=0; i<n; ++i){
vector<int> tmp(m, 0);
int point=0;
for(int j=0; j<m; ++j){
if(board[j][i]>0){
tmp[point++]=board[j][i];
}
}
reverse(tmp.begin(), tmp.end());
for(int j=0; j<m; ++j){
board[j][i]=tmp[j];
}
}
}
int pop(void){
int popped=0;
int dx[]={0, 0, 1, 1};
int dy[]={0, 1, 0, 1};
vector<int> x, y;
for(int i=0; i<m-1; ++i){
for(int j=0; j<n-1; ++j){
int pv=board[i][j];
if(board[i][j+1]==pv && board[i+1][j]==pv && board[i+1][j+1]==pv && pv){
x.push_back(i); y.push_back(j);
}
}
}
if(x.size()==0) return 0;
else {
for(int i=0; i<x.size(); ++i){
for(int j=0; j<4; ++j){
if(board[x[i]+dx[j]][y[i]+dy[j]]!=-1){
popped++;
board[x[i]+dx[j]][y[i]+dy[j]]=-1;
}
}
}
}
return popped;
}
void draw(void){
cout<<"Board: "<<endl;
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
cout<<board[i][j];
}
cout<<endl;
}
cout<<endl;
}
};
int solution(int m, int n, vector<string> board){
int answer=0, popped;
block blk(n, m, board);
do{
popped=blk.pop();
answer+=popped;
blk.drop();
}while(popped);
return answer;
}
int main(void){
int m1=4, m2=6, n1=5, n2=6;
vector<string> board1={"CCBDE", "AAADE", "AAABF", "CCBBF"};
vector<string> board2={"TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"};
int result1=solution(m1, n1, board1);
int result2=solution(m2, n2, board2);
cout<<result1<<endl;
cout<<result2<<endl;
return 0;
}
1> shuttle bus (Lv. 3)
https://school.programmers.co.kr/learn/courses/30/lessons/17678
#include <iostream>
#include <string>
#include <vector>
#include <format>
using namespace std;
struct Time{
int hour, min;
Time(){}
Time(string t){
hour=stoi(t.substr(0, 2));
min=stoi(t.substr(3, 2));
}
string now(void){
char h[3], m[3];
snprintf(h, 3, "%02d", hour); snprintf(m, 3, "%02d", min);
string t=string(h)+":"+string(m);
return t;
}
Time after(int t){
min+=t;
if(min>=60){
hour+=min/60;
min%=60;
} else if(min<0){
hour-=min/60+1;
while(min<0) min=60+min;
}
Time tmp(this->now());
return tmp;
}
bool is_before(const Time* other){
if(this->hour<other->hour) {
return true;
} else if(this->hour==other->hour){
if(this->min<other->min) return true;
} else {
return false;
}
return false;
}
};
string solution(int n, int t, int m, vector<string> Timetable){
sort(Timetable.begin(), Timetable.end());
string answer="";
vector<Time> arrive_bus;
vector<Time> line;
Time bus("09:00");
for(int i=0; i<n; ++i){
arrive_bus.push_back(bus);
bus.after(t);
}
for(int i=0; i<Timetable.size(); ++i){
line.push_back(Time(Timetable[i]));
}
int index=0;
for(int i=0; i<arrive_bus.size(); ++i){
int maxppl=m;
for(int j=index; j<line.size(); ++j){
if(!arrive_bus[i].is_before(&line[j]) && maxppl){
index++;
maxppl--;
if(i==arrive_bus.size()-1 && !maxppl) {
answer=line[j].after(-1).now();
return answer;
}
} else break;
}
}
answer=arrive_bus[arrive_bus.size()-1].now();
return answer;
}
int main(void){
int n1=1, t1=1, m1=5, n2=2, t2=10, m2=2, n3=2, t3=1, m3=2;
int n4=1, t4=1, m4=5, n5=1, t5=1, m5=1, n6=10, t6=60, m6=45;
vector<string> Timetable1={"08:00", "08:01", "08:02", "08:03"};
vector<string> Timetable2={"09:10", "09:09", "08:00"};
vector<string> Timetable3={"09:00", "09:00", "09:00", "09:00"};
vector<string> Timetable4={"00:01", "00:01", "00:01", "00:01", "00:01"};
vector<string> Timetable5={"23:59"};
vector<string> Timetable6={"23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"};
string result1=solution(n1, t1, m1, Timetable1);
string result2=solution(n2, t2, m2, Timetable2);
string result3=solution(n3, t3, m3, Timetable3);
string result4=solution(n4, t4, m4, Timetable4);
string result5=solution(n5, t5, m5, Timetable5);
string result6=solution(n6, t6, m6, Timetable6);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
cout<<"result3: "<<result3<<endl;
cout<<"result4: "<<result4<<endl;
cout<<"result5: "<<result5<<endl;
cout<<"result6: "<<result6<<endl;
return 0;
}
1> system Traffic (Lv. 3)
https://school.programmers.co.kr/learn/courses/30/lessons/17676
not solved yet
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int solution(vector<string> lines){
int answer=0;
return answer;
}
int main(void){
vector<string> input1={
"2016-09-15 01:00:04.001 2.0s",
"2016-09-15 01:00:07.000 2s"
};
vector<string> input2={
"2016-09-15 01:00:04.002 2.0s",
"2016-09-15 01:00:07.000 2s"
};
vector<string> input3={
"2016-09-15 20:59:57.421 0.351s",
"2016-09-15 20:59:58.233 1.181s",
"2016-09-15 20:59:58.299 0.8s",
"2016-09-15 20:59:58.688 1.041s",
"2016-09-15 20:59:59.591 1.412s",
"2016-09-15 21:00:00.464 1.466s",
"2016-09-15 21:00:00.741 1.581s",
"2016-09-15 21:00:00.748 2.31s",
"2016-09-15 21:00:00.966 0.381s",
"2016-09-15 21:00:02.066 2.62s"
};
int result1, result2, result3;
result1=solution(input1);
result2=solution(input2);
result3=solution(input3);
cout<<"Result1: "<<result1<<endl;
cout<<"Result2: "<<result2<<endl;
cout<<"Result3: "<<result3<<endl;
return 0;
}
3> Compressing (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/17684
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
vector<int> solution(string msg){
vector<int> answer;
int alp=65, size, last_index=26;
map<string, int> dict;
for(int i=0; i<26; ++i){
string alpha{static_cast<char>(alp+i)};
dict[alpha]=(i+1);
}
string buf;
for(int i=0; i<msg.length();){
size=msg.length()-i;
do{
string buf=msg.substr(i, size);
if(dict.find(buf)!=dict.end() && i+size<=msg.length()){
alp=dict[buf];
buf=msg.substr(i, size+1);
dict[buf]=++last_index;
i+=size;
break;
}
size--;
}while(1);
answer.push_back(alp);
}
return answer;
}
template <typename T>
void print_vec(vector<T> vec){
if(vec.size()==0){
cout<<"Vector Length 0"<<endl;
return;
}
cout<<"[";
for(int i=0; i<vec.size()-1; ++i){
cout<<vec[i]<<", ";
}
cout<<vec[vec.size()-1]<<"]"<<endl;
}
int main(void){
string msg1="KAKAO";
string msg2="TOBEORNOTTOBEORTOBEORNOT";
string msg3="ABABABABABABABAB";
vector<int> result1, result2, result3;
result1=solution(msg1);
result2=solution(msg2);
result3=solution(msg3);
print_vec<int>(result1);
print_vec<int>(result2);
print_vec<int>(result3);
}
3>  antilogarithm game
https://school.programmers.co.kr/learn/courses/30/lessons/17687
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string ten_n(int n, int num){
vector<int> num_pack;
char cnum;
do{
num_pack.push_back(num%n);
num/=n;
} while(num);
reverse(num_pack.begin(), num_pack.end());
string str;
for(int i=0; i<num_pack.size(); ++i){
switch(num_pack[i]){
case 10:
cnum='A';
break;
case 11:
cnum='B';
break;
case 12:
cnum='C';
break;
case 13:
cnum='D';
break;
case 14:
cnum='E';
break;
case 15:
cnum='F';
break;
default:
cnum=static_cast<char>(num_pack[i]);
cnum+='0';
}
string cstr{cnum};
str+=cstr;
}
return str;
}
string num_set(int n, int size){
string str;
int num=0;
do{
str+=ten_n(n, num);
num++;
}while(str.length()<size);
return str;
}
string solution(int n, int t, int m, int p){
string answer="", str;
str=num_set(n, t*m);
for(int i=p-1; i<t*m; i+=m){
answer+=str[i];
}
return answer;
}
int main(void){
string result1, result2, result3;
result1=solution(2, 4, 2, 1);
result2=solution(16, 16, 2, 1);
result3=solution(16, 16, 2, 2);
cout<<"result1: "<<result1<<endl;
cout<<"result2: "<<result2<<endl;
cout<<"result3: "<<result3<<endl;
return 0;
}
3> sorting Filename (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/17686
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct file{
string head, tail;
int num, ord;
file(){}
file(string file_name, int _ord): ord(_ord){
int i=0, j=0;
while(file_name[i]<'0' || file_name[i]>'9'){
i++;
}
head=file_name.substr(0, i);
for(int k=0; k<head.length(); ++k){
head[k]=tolower(head[k]);
}
j=i;
while(file_name[j]>='0' && file_name[j]<='9'){
j++;
}
num=stoi(file_name.substr(i, j-i));
tail=file_name.substr(j, file_name.length()-j+1);
}
};
bool crit(file f1, file f2){
if(f1.head.compare(f2.head)==0){
//if(f1.num==f2.num) return f1.tail<f2.tail;
return f1.num<f2.num;
} else return f1.head<f2.head;
}
vector<string> solution(vector<string> files){
vector<string> answer;
vector<file> files_vec;
for(int i=0; i<files.size(); ++i){
files_vec.push_back(file(files[i], i));
}
sort(files_vec.begin(), files_vec.end(), crit);
for(int i=0; i<files.size(); ++i){
answer.push_back(files[files_vec[i].ord]);
}
return answer;
}
template <typename T>
void print_vec(vector<T> vec){
if(!vec.size()) {
cout<<"Vector size 0"<<endl;
return;
}
cout<<"[";
for(int i=0; i<vec.size()-1; ++i){
cout<<vec[i]<<", ";
}
cout<<vec[vec.size()-1]<<"]"<<endl;
}
int main(void){
vector<string> input1={"img12.png", "img10.png", "img02.png", "img1.png", "IMG01.GIF", "img2.JPG"};
vector<string> input2={"F-5 Freedom Fighter", "B-50 Superfortress", "A-10 Thunderbolt II", "F-14 Tomcat"};
vector<string> result1, result2;
result1=solution(input1); result2=solution(input2);
print_vec(result1);
print_vec(result2);
return 0;
}
3> Song that I have heard (Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/17683